package com.ui.zhixiao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ui.zhixiao.common.R;
import com.ui.zhixiao.domain.Answer;
import com.ui.zhixiao.domain.Article;
import com.ui.zhixiao.domain.Category;
import com.ui.zhixiao.dto.AnswerDto;
import com.ui.zhixiao.service.AnswerService;
import com.ui.zhixiao.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/answer")
public class AnswerController {
    @Autowired
    private AnswerService answerService;

    @Autowired
    private CategoryService categoryService;


    /**
     * 新增答题
     * @param answer
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Answer answer){
        answerService.save(answer);
        return R.success("新增答题成功");
    }

    /**
     * 答题分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //构造分页
        Page<Answer> pageInfo = new Page<>(page,pageSize);
        Page<AnswerDto> answerDtoPage = new Page<>();
        //条件构造
        LambdaQueryWrapper<Answer> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.like(name!=null,Answer::getName,name);
        //排序
        queryWrapper.orderByDesc(Answer::getUpdateTime);

        //执行
        answerService.page(pageInfo);
        //对象copy
        BeanUtils.copyProperties(pageInfo,answerDtoPage,"records");

        List<Answer> records = pageInfo.getRecords();
        List<AnswerDto> list = records.stream().map((item)->{
            AnswerDto answerDto = new AnswerDto();
            BeanUtils.copyProperties(item,answerDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if(category!=null){
                String categoryName = category.getName();
                answerDto.setCategoryName(categoryName);
            }
            return answerDto;
        }).collect(Collectors.toList());

        answerDtoPage.setRecords(list);
        return R.success(answerDtoPage);
    }

    /**
     * 根据id查答题信息 回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Answer> get(@PathVariable Long id){
        Answer answer = answerService.getById(id);
        if(answer!=null){
            return R.success(answer);
        }
        return R.error("没有查询到答题信息");
    }

    /**
     * 根据id修改答题信息
     * @param answer
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Answer answer){
        answerService.updateById(answer);
        return R.success("修改答题信息成功");
    }

    /**
     * 删除 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(String ids){
        String[] split = ids.split(",");
        List<Long> idList = Arrays.stream(split).map(s->Long.parseLong(s.trim())).collect(Collectors.toList());
        answerService.removeByIds(idList);
        return R.success("删除成功");
    }

}
